//
//  Method.m
//  
//
//  Created by EvanZ on 14-3-7.
//  Copyright (c) 2014年 EvanZ. All rights reserved.
//

#import "Methods.h"
#import <CommonCrypto/CommonDigest.h>

@implementation Methods

#pragma mark - md5
+ (NSString *)md5:(NSString *)str
{
    
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

+ (NSString *)imageToNSString:(UIImage *)image
{
    NSData *imageData = UIImagePNGRepresentation(image);
    
    return [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

+ (UIImage *)stringToUIImage:(NSString *)string
{
    NSData *data = [[NSData alloc]initWithBase64EncodedString:string
                                                      options:NSDataBase64DecodingIgnoreUnknownCharacters];
    
    return [UIImage imageWithData:data];
}

#pragma mark - network
+ (BOOL)isExistenceNetwork{
    BOOL isExistenceNetwork;
    
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    
    switch ([reachability currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = FALSE;
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = TRUE;
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = TRUE;
            break;
        default:
            isExistenceNetwork = FALSE;
            break;
    }
    
    if (!isExistenceNetwork) {
        //        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"没用可用网络" message:@"请检查网络连接" delegate:self cancelButtonTitle:@"确认" otherButtonTitles: nil];
        //        [alertView show];
        //        [alertView release];
    }
    return  isExistenceNetwork;
}


+ (BOOL) checkNetworkIsValid {
    
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;
    
    
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;
    
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);
    
    if (!didRetrieveFlags) {
        return NO;
    }
    
    BOOL isReachable = flags & kSCNetworkFlagsReachable;
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
    // = flags & kSCNetworkReachabilityFlagsIsWWAN;
    BOOL nonWifi = flags & kSCNetworkReachabilityFlagsTransientConnection;
    BOOL moveNet = flags & kSCNetworkReachabilityFlagsIsWWAN;
    
    return ((isReachable && !needsConnection) || nonWifi || moveNet) ? YES : NO;
    
}

+ (NSDictionary *)handleDic:(NSDictionary*)dic{
    NSMutableDictionary * newDic = [[NSMutableDictionary alloc]init];
    for (NSString *key in dic) {
        if ([dic[key] isKindOfClass:[NSArray class]]) {
            [newDic setObject:[self handleArray:dic[key]] forKey:key];
        }
        else if([dic[key] isKindOfClass:[NSDictionary class]]){
            [newDic setObject:[self handleDic:dic[key]] forKey:key];
        }
        else if (strIsNull(dic[key])){
            [newDic setValue:@"" forKey:key];
        }
        else{
            [newDic setValue:dic[key] forKey:key];
        }
    }
    return newDic;
}

+ (NSArray *)handleArray:(NSArray*)array{
    NSMutableArray * newArray = [[NSMutableArray alloc]init];
    for(id obj in array){
        if ([obj isKindOfClass:[NSArray class]]) {
            [newArray addObject:[self handleArray:obj]];
        }
        else if ([obj isKindOfClass:[NSDictionary class]]){
            [newArray addObject:[self handleDic:obj]];
        }
        else if (strIsNull(obj)){
            [newArray addObject:@""];
        }
        else{
            [newArray addObject:obj];
        }
    }
    return newArray;
}

+ (id)safeResponseObj:(id)responseObj{
    if ([responseObj isKindOfClass:[NSArray class]]) {
        return [self handleArray:responseObj];
    }
    else if ([responseObj isKindOfClass:[NSDictionary class]]){
        return [self handleDic:responseObj];
    }
    else if(strIsNull(responseObj)){
        return @"";
    }
    else{
        return responseObj;
    }
}

@end
